package logic.cmd
{
	import common.core.GEevnt;
	import common.manager.ObserverEvt;
	
	import component.untils.DeserializeUIXML;
	import component.hide.FileReference2;
	import component.show.containers.UIFrame;
	import component.show.control.UISprite;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import component.untils.ComponentItem;

	public class OpenUIXMLCMD
	{
		public static const NAME  : String = "OPENUIXMLCMD";
		public static const OPENUIXML : String = "OpenUIXMLCMD_OpenUIXML";
		public static const OPENCOMPLETE : String = "OpenUIXMLCMD_OpenComplete";
		
		private var m_componentGroup : Array;
		
		private var m_uiFrame : UIFrame;
		
		public function OpenUIXMLCMD()
		{
			ObserverEvt.addRec(OPENUIXML,this,onOpenUIXML);
		}
		
		/**
		 * 收到打开UI XML的消息
		 * @param evt 
		 */		
		private function onOpenUIXML(evt : GEevnt) : void
		{
			m_componentGroup = evt.data["componentGroup"];
			
			var rootItem : ComponentItem = evt.data as ComponentItem;
			
			var file : FileReference2 = new FileReference2;
			
			file.open();
			file.addEventListener(Event.COMPLETE,openUIXMLComplete);
		}
		
		/**
		 * 成功打开XML
		 * @param evt 
		 */		
		private function openUIXMLComplete(evt : Event) : void
		{
			var file : FileReference2 = evt.currentTarget as FileReference2;
			var rootItem : ComponentItem;
			
			file.removeEventListener(Event.COMPLETE,openUIXMLComplete);
			
			var comxml : XML = new XML(file.data);
			var comGroup : Array = [];
			
			var deserializeUIXML : DeserializeUIXML = new DeserializeUIXML;
			deserializeUIXML.recursionXML(comxml,m_uiFrame);
			
			rootItem  = deserializeUIXML.rootItem;
			m_uiFrame = rootItem.item;
			m_uiFrame.collection = deserializeUIXML.collection;
			
			comGroup.push(rootItem);
			
			m_componentGroup.concat(comGroup);
			
			var content : Object = new Object;
			
			content["uiFrame"] = m_uiFrame;
			content["componentGroup"] = m_componentGroup;
			content["rootItem"] = deserializeUIXML.rootItem;
			
			deserializeUIXML.destory();
			
			ObserverEvt.Send(OPENCOMPLETE,content);
		}
	}
}